﻿using Microsoft.Practices.Unity;
using RockBus.DataContracts;
using RockBus.Initialization.Interfaces;
using RockBus.Logging.Interfaces;
using RockBus.Routing;
using RockBus.Routing.Interfaces;
using RockBus.Routing.SendPorts;
using RockBus.Transport.File;
using RockBus.Transport.Interfaces;
using RockBus.Transport.Wcf;

namespace RockBus.Composition
{
    public class InMemorySubscriptionsFileTransportDependencyManager : EntLibSubscriptionCachingMsmqMessagePersistenceDependencyManager
    {
        protected override void FillContainer(IUnityContainer container)
        {
            base.FillContainer(container);

            container

            // Overwrite Transports to include FileTransport
            .RegisterType<ITransportFactory, TransportFactory>(TransportTypeEnum.WcfTransport.ToString(), new ContainerControlledLifetimeManager())
            .RegisterType<ITransportManager, TransportManager>(new ContainerControlledLifetimeManager(),
                new InjectionConstructor(
                    new ResolvedParameter<ILogger>(),
                    new ResolvedArrayParameter<ITransportFactory>(
                        new ResolvedParameter<ITransportFactory>(TransportTypeEnum.WcfTransport.ToString()),
                        new ResolvedParameter<ITransportFactory>(TransportTypeEnum.FileTransport.ToString()))))
            .RegisterType<ITransportFactory, FileTransportFactory>(TransportTypeEnum.FileTransport.ToString(), new ContainerControlledLifetimeManager())
            .RegisterType<IMessagePublisher, FileMessagePublisher>(InstanceNames.FileMessagePublisher, new ContainerControlledLifetimeManager())
            .RegisterType<IPublisherManager, PublisherManager>(new ContainerControlledLifetimeManager(),
                new InjectionConstructor(
                    new ResolvedParameter<ILogger>(),
                    new ResolvedArrayParameter<IMessagePublisher>(
                        new ResolvedParameter<IMessagePublisher>(InstanceNames.FileMessagePublisher))))
            .RegisterType<IInitialization, PublisherManagerInitialization>(InstanceNames.PublisherManagerInitialization, new ContainerControlledLifetimeManager())
            .RegisterType<IMessageProcessor, InMemoryErrorProcessor>(InstanceNames.ErrorManager, new ContainerControlledLifetimeManager())
            ;
        }
    }
}